﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Notoric.Model.Elements;

namespace Notoric.Model.Packaging
{
    /// <summary>
    /// Implements an XML package part that holds the data of a page model element (<see cref="PageElement"/>).
    /// </summary>
    public sealed class PagePart : XmlPackagePart
    {
        #region Constructors

        /// <summary>
        /// Creates a new <see cref="PagePart"/> instance.
        /// </summary>
        /// <param name="package">
        /// <para>
        /// Type: <see cref="Package"/>
        /// </para>
        /// <para>
        /// The parent package (the package that owns this part).
        /// </para>
        /// </param>
        /// <param name="owner">
        /// <para>
        /// Type: <see cref="ScorePart"/>
        /// </para>
        /// <para>
        /// The score package part that is the logical owner of this part.
        /// This must not be <see langword="null"/>.
        /// </para>
        /// </param>
        /// <param name="implementationPart">
        /// <para>
        /// Type: <see cref="System.IO.Packaging.PackagePart"/>
        /// </para>
        /// <para>
        /// An existing part of the parent's <see cref="Notoric.Model.Packaging.Package.ImplementationPackage"/> that implements
        /// the core functionality of this part.
        /// </para>
        /// </param>
        /// <param name="createNew">
        /// <para>
        /// Type: <see cref="System.Boolean"/>
        /// </para>
        /// <para>
        /// Indicates whether the content of this part should be created from scratch or loaded from the part content.<br/>
        /// <see langword="true"/> means create a new part content, <see langword="false"/> means load the content from
        /// the physical storage.
        /// </para>
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="package"/>, <paramref name="owner"/> or <paramref name="implementationPart"/> is <see langword="null"/>.
        /// </exception>
        internal PagePart(Package package, ScorePart owner, System.IO.Packaging.PackagePart implementationPart, bool createNew)
            : base(package, owner, implementationPart, createNew)
        {
            if (owner == null)
                throw new ArgumentNullException("owner");
        }

        #endregion

        #region XmlPackagePart Members

        /// <inheritdoc/>
        protected override ModelElement CreateAndLoadRoot(XmlElement element)
        {
            return new PageElement(this, element);
        }

        /// <inheritdoc/>
        protected override ModelElement CreateNewRoot()
        {
            return new PageElement(this);
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets the root <see cref="PageElement"/> of the part
        /// (Overrides the <see cref="XmlPackagePart.RootElement"/>).
        /// </summary>
        public PageElement Page
        {
            get { return (PageElement)RootElement; }
        }

        #endregion
    }
}
